<?php

/**
 * Inflect string to CamelCase string
 * @param   string   $string
 * @return  string
 */
function inflect($string)
{
	return implode('_', array_map('ucfirst', explode('/', $string)));
}

/**
 * Deflect string to words split string
 * @param   string  $string
 * @return  string
 */
function deflect($string)
{
	return strtolower(trim(preg_replace('/([a-z0-9])([A-Z])/', '\1-\2', $string), '-. '));
}

/**
 * Format date to string
 * @param   int  $time
 * @return  string
 */
function format_date($time)
{
	return date('d/m/Y', $time);
}

/**
 * Format date to string
 * @param   int  $time
 * @return  string
 */
function format_datetime($time)
{
	return date('Y-m-d H:i:s', $time);
}

/**
 * Get translated message
 * @param   string  $msg
 * @param   array   $data  Optional
 * @return  string
 */
function __text($msg, $data = array())
{

	return Intl::__($msg, $data);
}

/**
 * Get privacy string
 * @param   int   $val
 * @return  string
 */
function get_privacy_label($val)
{
	switch($val)
	{
		case PRIVACY_PUBLIC :
			return Intl::__('Public');
		case PRIVACY_FRIENDS :
			return Intl::__('Friends');
		case PRIVACY_FRIENDS_OF_FRIENDS :
			return Intl::__('Friends of Friends');
		case PRIVACY_PRIVATE :
			return Intl::__('Private');
		case PRIVACY_LISTS :
			return Intl::__('Custom List');
		case PRIVACY_NETWORKS :
			return Intl::__('Custom Networks');
		default :
			return Intl::__('Custom Privacy');
	}
}

/**
 * Get slug string
 * Replace space character by "-"
 * @param   string  $string
 * @return  string
 */
function to_slug($string)
{
	return preg_replace("#\s+#", '-', (string)$string);
}

/**
 * Check string include digit character only
 * @param   string  $string
 * @return  bool
 */
function is_id($string)
{
	return preg_match("#^\d+$#", $string);
}

/**
 * Flattern array of html element attribute to string
 * @param   array  $params
 * @return  string
 */
function flat_html($params)
{

	$xhtml = array();
	foreach ($params as $key => $value)
	{
		$xhtml[] = $key . '="' . $value . '"';
	}
	return implode(' ', $xhtml);
}

/**
 * Quote html string to jscript
 * @param   string  $html
 * @return  string
 */
function quote_html($html)
{
	return str_replace('"', '\"', str_replace('\\', '\\\\', str_replace("\r", '', str_replace("\n", '', $html))));
}

/**
 * Get random text string
 * @param   int  $len
 * @param   string $type  avalable options: secret, alpha, lower, upper, num, alnum
 * @return  string
 */
function get_random_text($len = 8, $type = 'alnum')
{
	switch($type)
	{
		case 'secret' :
			$seek = '1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM*~!@#$%^&*-+_=|,>';
			break;
		case 'alpha' :
			$seek = 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM';
			break;
		case 'lower' :
			$seek = 'qwertyuiopasdfghjklzxcvbnm';
			break;
		case 'upper' :
			$seek = 'QWERTYUIOPASDFGHJKLZXCVBNM';
			break;
		case 'num' :
			$seek = '0123456789';
			break;
		case 'alnum' :
		default :
			$seek = '1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM';
	}

	$max = strlen($seek) - 1;

	$ret = '';

	for ($i = 0; $i < $len; ++$i)
	{
		$ret .= substr($seek, mt_rand(0, $max), 1);
	}
	return $ret;
}
